<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index2.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <form action="" name="frm">
    	<input type="text" name="username"><label id="lab1"></label>
    </form>
    <script type="text/javascript">
    document.frm.username.onblur=function(){
    	//1
    	var xhr=new XMLHttpRequest();
    	//2
    	xhr.open("post", "/lesson/BServlet",true);
    	//3
    	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    	//4
    	var data=document.frm.username.value;
    	xhr.send("username="+data);
    	xhr.onreadystatechange=function(){
    		if(xhr.status==200&&xhr.readyState==4){
    			var res=xhr.responseText;
    			if(res==0){
    				document.getElementById("lab1").innerHTML="用户名可以使用!!";
    				document.getElementById("lab1").style.color="green";
    			}else{
    				document.getElementById("lab1").innerHTML="用户名已存在!!";
    				document.getElementById("lab1").style.color="red";
    			}
    		}
    	}
    }		
    </script>
  </body>
</html>
